package com.android.mios;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.android.mios.adapter.LightGridViewAdapter;
import com.android.mios.intenet.PollLoop;
import com.android.mios.model.Category;
import com.android.mios.model.Device;
import com.android.mios.util.Global;

import android.os.Bundle;
import android.widget.GridView;

public class LightControlActivity extends DeviceBaseActivity {

	private List<Device> lightOfArray;
	private GridView mGridView;
	private LightGridViewAdapter mLightGridViewAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.light_contrl_layout);
		initData();
		initView();
	}

	private void initData() {

		lightOfArray = new ArrayList<Device>();
		mLightGridViewAdapter = new LightGridViewAdapter(this, lightOfArray,
				R.layout.light_contrl_item);

	}

	private void initView() {

		initGridView();
	}

	private void initGridView() {

		mGridView = (GridView) findViewById(R.id.GV_LightControl);
		mGridView.setAdapter(mLightGridViewAdapter);

	}

	@Override
	protected List<Device> getDeviceList(int serialNumber,int roomId) {
		// TODO Auto-generated method stub
		Map<String, Device> allDevices = PollLoop.getInstance().getDevices();

		List<Device> devices = Device.findUnHideDevicesByCategory(allDevices, Category.SWITCH);
		devices.addAll(Device.findUnHideDevicesByCategory(allDevices, Category.DIMMABLE_LIGHT));

		if (roomId != -1)// -1 is all room
			devices = Device.findDevicesByRoom(devices,serialNumber, roomId);

		return devices;
	}

	@Override
	protected void updateDevice() {
		// TODO Auto-generated method stub
		lightOfArray.clear();
		lightOfArray.addAll(getDeviceList(Global.getInstance().getSerialNumber(),Global.getInstance().getRoomID()));
		mLightGridViewAdapter.notifyDataSetChanged();
	}

}
